import React from 'react';

import BaseExecuter from './BaseExecuter'
import DataManager from '../../datas/DataManager';
import Constants from '../../common/Constants';
import ToastUtils from '../../common/ToastUtils';
import Routes from '../../common/Routes';

export default class CommonExecuter extends BaseExecuter {
  constructor(props) {
    super(props);

    this._stage = props.stage;
  }

  execute(action) {
    switch (action.name) {
      case "ChangePageAction":
        this._changePageAction(action);
        break;
      case "JumpToPageByUrlAction":
        this._jumpToPageByUrl(action);
        break;
      case "JumpToPageByNotification":
        this._jumpToPageNotification(action);
        break;
      case "CommonRequestSliderImagesAction":
        this._requestSlider(action);
        break;
      case "CommonRequestADAction":
        this._requestADSlider(action);
        break;
      case "CommonRequestUrlAction":
        this._requestUrl(action);
        break;
      case "CommonCheckVersionAction":
        this._checkVersion(action);
        break;
      case "CommonCheckUserTypeAction":
        this._checkUserType(action);
        break;
      case "CommonShowImageFullScreenAction":
        this._showImageFS(action);
        break;
      case "CommonShowOptionAction":
        this._showOption(action);
        break;
      case "CommonLiveAction":
        this._showLive(action);
        break;
      case "CommonShowPickerAction":
        this._showPicker(action);
        break;
      case "CommonShowVideoAction":
        this._showViewVideo(action);
        break;
      case "CommonShowViewFullScreenAction":
        this._showViewFullScreen(action);
        break;
      case "SharePointAction":
        this._sharePoint(action);
        break;
      case "CommonCheckNeedGoNotificationAction":
        this._checkNeedNotification(action);
        break;
      case "CommonCallTellAction":
        this._callTell(action);
        break;
    }
  }

  _callTell(action) {
    if(action.data) {
      var phoneNum = parseInt(action.data);
      if(phoneNum && phoneNum > 0) {
        action.finish(phoneNum);
      }
      else {
        ToastUtils.show("电话号码不合法");
      }
    }
  }

  _checkNeedNotification(action) {

  }

  _showViewFullScreen(action) {
    action.finish(this._stage);
  }
  _showViewVideo(action) {
    action.finish(this._stage);
  }

  _showPicker(action) {
    action.finish(this._stage);
  }

  _showOption(action) {
    action.finish(this._stage);
  }
  _showLive(action) {
    var data = action.data;
    console.log(action)
    DataManager.callMethod(Constants.MODULE_TYPE.COMMON,
      "requestLive",
      [data.tag, (data, isSucceed, response)=>{action.finish(data, isSucceed, response)}]);
  }
  _showImageFS(action) {
    action.finish(this._stage);
  }

  _checkUserType(action) {
    let selfData = DataManager.callMethod(Constants.MODULE_TYPE.USER,
      "getSelfData");
    let result = true;
    let type = 0;
    if(action.data.needLogin) {
      result = result && selfData.nick != "" && selfData.nick != null;
      if(!result) {
        type = 1;
      }
    }
    if(result && action.data.needVerify) {
      result = result && selfData.name != "" && selfData.name != null;
      if(!result) {
        type = 2;
      }
    }
    action.finish(result, type);
  }

  _checkVersion(action) {
    let versionData = DataManager.callMethod(Constants.MODULE_TYPE.COMMON,
      "getSaveVersion");
    action.finish(versionData[action.data.versionType] == action.data.versionValue);
  }

  _requestUrl(action) {
    this._sendRequestCommonResponse(action, "requestUrlForContent");
  }

  _requestSlider(action) {
    var data = action.data;
    DataManager.callMethod(Constants.MODULE_TYPE.COMMON,
      "requestSildersImages",
      [data.tag, (data, isSucceed, response)=>{action.finish(data, isSucceed, response)}]);
  }
  _requestADSlider(action){
    this._sendRequestCommonResponse(action, "requestADSildersImages");
    // var data = action.data;
    // DataManager.callMethod(Constants.MODULE_TYPE.COMMON,
    //   "requestADSildersImages",
    //   [data, (data, isSucceed, response)=>{action.finish(data, isSucceed, response)}]);
  }
  _sharePoint(action){
    var data = action.data;
    DataManager.callMethod(Constants.MODULE_TYPE.COMMON,
      "sharePoint",
      [data, (data, isSucceed, response)=>{action.finish(data, isSucceed, response)}]);
  }


  _jumpToPageByUrl(action) {
    let data = action.data;
    if(data){
      action.finish(data);
      this._sendRequestCommonResponse(action, "slideClick");
    }
  }

  _jumpToPageNotification(action) {
    action.finish();
  }

  _changePageAction(action) {
    var actionData = action.data;
    if(actionData.type == "goto") {
      Routes.jumpToPage(actionData.nextPageData, actionData.notSave);
    }
    else if (actionData.type == "goBack") {
      Routes.goBack();
    }
    else if(actionData.type == "goBackIndex"){
      Routes.goBackIndex(actionData.index);
    }
    else if(actionData.type == "popRoute"){
      Routes.popRoute(actionData.index);
    }
  }

  _getModuleType() {
    return Constants.MODULE_TYPE.COMMON;
  }
}
